package weaver.interfaces.cyjt.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cyjt.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-12-11
 * @Description: OA单据：01.事项审批表（生成NC单据）
 * @ModifyLog:
 **/
public class WeaSxspd01Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaSxspd01Action>>>>>requestid=" + requestid);
        try {
            String message = "金蝶接口返回：";
            RecordSet rs = new RecordSet();
            KingdeeUtil kingdeeUtil = new KingdeeUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String cwlx = Util.null2String(rs.getString("cwlx"));
            //申请日期
            String sqrq = Util.null2String(rs.getString("sqrq"));
            //结算方式.编码
            String jsfsbm = Util.null2String(rs.getString("jsfsbm"));
            //付款币种.货币代码
            String bzbm = Util.null2String(rs.getString("bzbm"));
            //收款人类型
            String skrbm = Util.null2String(rs.getString("skrbm"));
            //付款金额折本位币
            String bcfkhj = Util.null2String(rs.getString("bcfkhj"));
            //收款人类型-客户
            String khbm = Util.null2String(rs.getString("khbm"));
            //收款人类型-供应商
            String gysbm = Util.null2String(rs.getString("gysbm"));
            //银行账号.id
            String fkyhzhbm = Util.null2String(rs.getString("fkyhzhbm"));
            //收款账号
            String skyhzhbm = Util.null2String(rs.getString("skyhzhbm"));
            //付款人（公司）.id
            String org_id = Util.null2String(rs.getString("orgid"));
            //成本中心.编码
            String sqbmjdbm = Util.null2String(rs.getString("sqbmjdbm"));
            //收款单
            if ("0".equals(cwlx)) {
                //付款银行
                String fkyhmc = Util.null2String(rs.getString("fkyhmc"));
                //收款类型
                String sklxbm = Util.null2String(rs.getString("sklxbm"));
                JSONObject paramsJson = new JSONObject();
                JSONArray dataList = new JSONArray();
                JSONObject dataJson = new JSONObject();
                dataJson.put("biztype", "OtherRec");
                dataJson.put("bizdate", DateUtil.getCurrentDate());
                dataJson.put("org_id", org_id);
                dataJson.put("receivingtype_number", sklxbm);
                dataJson.put("settletype_number", jsfsbm);
                dataJson.put("currency_number", bzbm);
                dataJson.put("payertype", skrbm);
                dataJson.put("accountbank_id", skyhzhbm);
                dataJson.put("payerbankname", fkyhmc);
                dataJson.put("payeracctbanknum", fkyhzhbm);
                dataJson.put("actrecamt", bcfkhj);
                String payee = "";
                if ("bd_customer".equals(skrbm)) {
                    payee = khbm;
                }
                if ("bd_supplier".equals(skrbm)) {
                    payee = gysbm;
                }
                dataJson.put("payer", payee);
                JSONArray entryList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject entryJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    String fyxmbm = Util.null2String(rs.getString("fyxmbm"));
                    String FYSM = Util.null2String(rs.getString("FYSM"));
                    String bcfkje = Util.null2String(rs.getString("bcfkje"));
                    entryJson.put("e_expenseitem_number", fyxmbm);
                    entryJson.put("e_remark", FYSM);
                    entryJson.put("e_receivableamt", bcfkje);
                    entryJson.put("costcenter_number", sqbmjdbm);
                    entryList.add(entryJson);
                }
                dataJson.put("entry", entryList);
                dataList.add(dataJson);
                paramsJson.put("data", dataList);
                writeLog(">>>>WeaSxspd01Action>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = kingdeeUtil.handleKingdeeRecBill(paramsJson, requestid);
                writeLog(">>>>WeaSxspd01Action>>>>resultJson=" + resultJson);
                String retrunmessage = Util.null2String(resultJson.getString("message"));
                if ("success".equals(retrunmessage)) {
                    String datas = Util.null2String(resultJson.getString("datas"));
                    JSONObject returnDatas = JSONObject.parseObject(datas);
                    String errorCode = returnDatas.getString("errorCode");
                    if ("0".equals(errorCode)) {
                        return Action.SUCCESS;
                    } else {
                        message += returnDatas.getString("message");
                    }
                } else {
                    message += retrunmessage;
                }
            }
            //付款单
            if ("1".equals(cwlx)) {
                //付款类型
                String fklxbm = Util.null2String(rs.getString("fklxbm"));
                //收款银行名称
                String skyhmcdhwb = Util.null2String(rs.getString("skyhmcdhwb"));
                JSONObject paramsJson = new JSONObject();
                JSONArray dataList = new JSONArray();
                JSONObject dataJson = new JSONObject();
                dataJson.put("billtype_number", "cas_paybill_other_BT_S");
                dataJson.put("bizdate", DateUtil.getCurrentDate());
                dataJson.put("settletype_number", jsfsbm);
                dataJson.put("paymenttype_number", fklxbm);
                dataJson.put("currency_number", bzbm);
                dataJson.put("org_id", org_id);
                dataJson.put("payeracctbank_id", fkyhzhbm);
                dataJson.put("payeetype", skrbm);
                String payee = "";
                if ("bd_customer".equals(skrbm)) {
                    payee = khbm;
                }
                if ("bd_supplier".equals(skrbm)) {
                    payee = gysbm;
                }
                dataJson.put("payee", payee);
                dataJson.put("payeebanknum", skyhzhbm);
                dataJson.put("payeebankname", skyhmcdhwb);
                dataJson.put("localamt", bcfkhj);
                JSONArray entryList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject entryJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    String fyxmbm = Util.null2String(rs.getString("fyxmbm"));
                    String FYSM = Util.null2String(rs.getString("FYSM"));
                    String bcfkje = Util.null2String(rs.getString("bcfkje"));
                    entryJson.put("e_expenseitem_number", fyxmbm);
                    entryJson.put("e_remark", FYSM);
                    entryJson.put("e_payableamt", bcfkje);
                    entryJson.put("costcenter_number", sqbmjdbm);
                    entryList.add(entryJson);
                }
                dataJson.put("entry", entryList);
                dataList.add(dataJson);
                paramsJson.put("data", dataList);
                writeLog(">>>>WeaSxspd01Action>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = kingdeeUtil.handleKingdeePayBill(paramsJson, requestid);
                writeLog(">>>>WeaSxspd01Action>>>>resultJson=" + resultJson);
                String retrunmessage = Util.null2String(resultJson.getString("message"));
                if ("success".equals(retrunmessage)) {
                    String datas = Util.null2String(resultJson.getString("datas"));
                    JSONObject returnDatas = JSONObject.parseObject(datas);
                    String errorCode = returnDatas.getString("errorCode");
                    if ("0".equals(errorCode)) {
                        return Action.SUCCESS;
                    } else {
                        message += returnDatas.getString("message");
                    }
                } else {
                    message += retrunmessage;
                }
            }
            //应收单
            if ("2".equals(cwlx)) {
                //发票日期
                String fprq = Util.null2String(rs.getString("fprq"));
                //到期日
                String dqr = Util.null2String(rs.getString("dqr"));
                //客户
                String khbmjdbsid = Util.null2String(rs.getString("khbmjdbsid"));
                //供应商
                String gysbmjdbsid = Util.null2String(rs.getString("gysbmjdbsid"));
                //结算方式.编码
                String fkfsbm = Util.null2String(rs.getString("fkfsbm"));
                //款项性质.编码
                String kxxzbm = Util.null2String(rs.getString("kxxzbm"));
                //备注
                String BeiZhu = Util.null2String(rs.getString("BeiZhu"));
                //应收金额
                String JE = Util.null2String(rs.getString("JE"));

                JSONObject paramsJson = new JSONObject();
                JSONArray dataList = new JSONArray();
                JSONObject dataJson = new JSONObject();
                dataJson.put("billtype_number", "arfin_other_BT_S");
                dataJson.put("org_id", org_id);
                dataJson.put("bizdate", DateUtil.getCurrentDate());
                dataJson.put("invoicedate", fprq);
                dataJson.put("duedate", dqr);
                dataJson.put("asstacttype", skrbm);
                String asstact_number = "";
                if ("bd_customer".equals(skrbm)) {
                    asstact_number = khbmjdbsid;
                }
                if ("bd_supplier".equals(skrbm)) {
                    asstact_number = gysbmjdbsid;
                }
                dataJson.put("asstact_number", asstact_number);
                dataJson.put("recorg_id", org_id);
                dataJson.put("paymode", fkfsbm);
                dataJson.put("settlementtype_number", jsfsbm);
                dataJson.put("payproperty_number", kxxzbm);
                dataJson.put("remark", BeiZhu);
                dataJson.put("recamount", JE);
                dataJson.put("currency_number", bzbm);
                JSONArray entryList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject entryJson = new JSONObject();
                    String fyxmbm = Util.null2String(rs.getString("fyxmbm"));
                    String sl = Util.null2String(rs.getString("sl"));
                    String bcfkje = Util.null2String(rs.getString("bcfkje"));
                    entryJson.put("e_expenseitem_number", fyxmbm);
                    entryJson.put("e_taxunitprice", bcfkje);
                    entryJson.put("e_taxrate", sl);
                    entryList.add(entryJson);
                }
                dataJson.put("entry", entryList);
                dataList.add(dataJson);
                paramsJson.put("data", dataList);
                writeLog(">>>>WeaSxspd01Action>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = kingdeeUtil.handleKingdeeArfinarBill(paramsJson, requestid);
                writeLog(">>>>WeaSxspd01Action>>>>resultJson=" + resultJson);
                String retrunmessage = Util.null2String(resultJson.getString("message"));
                if ("success".equals(retrunmessage)) {
                    String datas = Util.null2String(resultJson.getString("datas"));
                    JSONObject returnDatas = JSONObject.parseObject(datas);
                    String errorCode = returnDatas.getString("errorCode");
                    if ("0".equals(errorCode)) {
                        return Action.SUCCESS;
                    } else {
                        message += returnDatas.getString("message");
                    }
                } else {
                    message += retrunmessage;
                }
            }
            //应付单
            if ("3".equals(cwlx)) {
                //应付金额
                String JE = Util.null2String(rs.getString("JE"));
                //付款方式
                String fkfsbm = Util.null2String(rs.getString("fkfsbm"));
                //到期日
                String dqr = Util.null2String(rs.getString("dqr"));
                //客户
                String khbmjdbsid = Util.null2String(rs.getString("khbmjdbsid"));
                //供应商
                String gysbmjdbsid = Util.null2String(rs.getString("gysbmjdbsid"));
                //款项性质.编码
                String kxxzbm = Util.null2String(rs.getString("kxxzbm"));
                //备注
                String BeiZhu = Util.null2String(rs.getString("BeiZhu"));

                JSONObject paramsJson = new JSONObject();
                JSONArray dataList = new JSONArray();
                JSONObject dataJson = new JSONObject();
                dataJson.put("billtypeid_number", "ApFin_other_BT_S");
                dataJson.put("pricetaxtotal", JE);
                dataJson.put("asstacttype", skrbm);
                dataJson.put("bizdate", DateUtil.getCurrentDate());
                dataJson.put("purmode", fkfsbm);
                dataJson.put("duedate", dqr);
                dataJson.put("org_id", org_id);
                String asstact_number = "";
                if ("bd_customer".equals(skrbm)) {
                    asstact_number = khbmjdbsid;
                }
                if ("bd_supplier".equals(skrbm)) {
                    asstact_number = gysbmjdbsid;
                }
                dataJson.put("asstact_number", asstact_number);
                dataJson.put("payorg_id", org_id);
                dataJson.put("payproperty_number", kxxzbm);
                dataJson.put("remark", BeiZhu);
                dataJson.put("settlementtype_number", jsfsbm);
                dataJson.put("currency_number", bzbm);
                JSONArray detailentryList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject entryJson = new JSONObject();
                    String fyxmbm = Util.null2String(rs.getString("fyxmbm"));
                    String sl = Util.null2String(rs.getString("sl"));
                    String bcfkje = Util.null2String(rs.getString("bcfkje"));
                    entryJson.put("expenseitem_number", fyxmbm);
                    entryJson.put("pricetax", bcfkje);
                    entryJson.put("taxrate", sl);
                    entryJson.put("e_costcenter_number", sqbmjdbm);
                    detailentryList.add(entryJson);
                }
                dataJson.put("detailentry", detailentryList);
                JSONArray preentryList = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt3 where mainid=?", mainid);
                while (rs.next()) {
                    JSONObject entryJson = new JSONObject();
                    String id = Util.null2String(rs.getString("id"));
                    String yfkdbm = Util.null2String(rs.getString("yfkdbm"));
                    String bcjsje = Util.null2String(rs.getString("bcjsje"));
                    entryJson.put("y_billno", yfkdbm);
                    entryJson.put("y_settleamt", bcjsje);
                    preentryList.add(entryJson);
                }
                dataJson.put("preentry", preentryList);
                dataList.add(dataJson);
                paramsJson.put("data", dataList);
                writeLog(">>>>WeaSxspd01Action>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = kingdeeUtil.handleKingdeeApfinapBill(paramsJson, requestid);
                writeLog(">>>>WeaSxspd01Action>>>>resultJson=" + resultJson);
                String retrunmessage = Util.null2String(resultJson.getString("message"));
                if ("success".equals(retrunmessage)) {
                    String datas = Util.null2String(resultJson.getString("datas"));
                    JSONObject returnDatas = JSONObject.parseObject(datas);
                    String errorCode = returnDatas.getString("errorCode");
                    if ("0".equals(errorCode)) {
                        return Action.SUCCESS;
                    } else {
                        message += returnDatas.getString("message");
                    }
                } else {
                    message += retrunmessage;
                }
            }

            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(message);
            return Action.FAILURE_AND_CONTINUE;
        } catch (Exception e) {
            writeLog(">>>>>WeaSxspd01Action>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }


}